import { Injectable } from '@angular/core'
import { Observable, of } from 'rxjs'
import { map, switchMap } from 'rxjs/operators'
import { NzTreeNode } from 'ng-zorro-antd'

import { RoleService } from './role.service'
import { TreeService } from '@shared/services/tree.service'
import { EMenuType } from '@shared/enums'

@Injectable()
export class RoleDataService {

  constructor(private roleService: RoleService, private treeService: TreeService) {}

  get menuTree$(): Observable<NzTreeNode[]> {
    return of(null).pipe(
      switchMap(() => this.roleService.getMenus()),
      map(menus => {
        const ms = menus.filter(m => m.menuStatus !== EMenuType.Public && m.menuStatus !== EMenuType.Protect)
        const tree = this.treeService.createTree(ms)
        return tree
      }),
    )
  }
}
